package com.genersoft.iot.vmp;

import com.genersoft.iot.vmp.utils.GitUtil;
import com.genersoft.iot.vmp.utils.SpringBeanFactory;
import jakarta.servlet.ServletContext;
import jakarta.servlet.ServletException;
import jakarta.servlet.SessionCookieConfig;
import jakarta.servlet.SessionTrackingMode;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.util.Collections;

//@ServletComponentScan("com.genersoft.iot.vmp.conf")
//@SpringBootApplication(scanBasePackages = "com.genersoft.iot.vmp")
@SpringBootApplication
@MapperScan(basePackages = {"com.genersoft.iot.vmp.storager.dao","com.genersoft.iot.vmp.gb28181.dao","com.genersoft.iot.vmp.streamPush.dao", "com.genersoft.iot.vmp.streamProxy.dao"})
@EnableScheduling
@EnableCaching
@Slf4j
public class VManageBootstrap extends SpringBootServletInitializer {

    private static String[] args;
    private static ConfigurableApplicationContext context;

    public static void main(String[] args) {
        VManageBootstrap.args = args;
        VManageBootstrap.context = SpringApplication.run(VManageBootstrap.class, args);
        GitUtil gitUtil = SpringBeanFactory.getBean("gitUtil");
        if (gitUtil == null) {
            log.info("获取版本信息失败");
        } else {
            log.info("构建版本： {}", gitUtil.getBuildVersion());
            log.info("构建时间： {}", gitUtil.getBuildDate());
            log.info("GIT信息： 分支: {}, ID: {},  时间: {}", gitUtil.getBranch(), gitUtil.getCommitIdShort(), gitUtil.getCommitTime());
        }
    }

    // 项目重启
    public static void restart() {
        context.close();
        VManageBootstrap.context = SpringApplication.run(VManageBootstrap.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(VManageBootstrap.class);
    }

    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {
        super.onStartup(servletContext);

        servletContext.setSessionTrackingModes(
                Collections.singleton(SessionTrackingMode.COOKIE)
        );
        SessionCookieConfig sessionCookieConfig = servletContext.getSessionCookieConfig();
        sessionCookieConfig.setHttpOnly(true);

    }
}
